package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.InterventionDao;
import com.itheima.pojo.Intervention;
import com.itheima.service.InterventionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = InterventionService.class)

public class InterventionImpl implements InterventionService {
    @Autowired
    private InterventionDao interventionDao;
    @Override
    public void add(Intervention intervention, Integer[] sportIds, Integer[] foodIds) {
        interventionDao.add(intervention);
        int interventionId = intervention.getId();
        if(sportIds!=null&&sportIds.length>0){
            for (Integer sportId : sportIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("intervention_id",interventionId);
                map.put("sports_id",sportId);
                interventionDao.setInterventionAndSports(map);
            }
        }

        if(foodIds!=null&&foodIds.length>0){
            for (Integer foodId : foodIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("intervention_id",interventionId);
                map.put("food_id",foodId);
                interventionDao.setInterventionAndFood(map);
            }
        }



    }

    @Override
    public Intervention findById(Integer id) {
        return  interventionDao.findById(id);
    }

    @Override
    public List<Integer> findsportsIdByOrderId(Integer id) {
        return interventionDao.findsportsIdByOrderId(id);
    }

    @Override
    public List<Integer> findfoodIdsByOrderId(Integer id) {
        return interventionDao.findfoodIdsByOrderId(id);
    }

    @Override
    public Intervention findByOrderId(Integer id) {
        return interventionDao.findByOrderId(id);
    }

    @Override
    public void edit(Intervention intervention, Integer[] sportsIds, Integer[] foodIds) {

        interventionDao.edit(intervention);
        interventionDao.deleteAssocicationSport(intervention.getId());
        interventionDao.deleteAssocicationFood(intervention.getId());
        int interventionId= intervention.getId();

        if(sportsIds!=null&&sportsIds.length>0){
            for (Integer sportId : sportsIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("intervention_id",interventionId);
                map.put("sports_id",sportId);
                interventionDao.setInterventionAndSports(map);
            }
        }
        if(foodIds!=null&&foodIds.length>0){
            for (Integer foodId : foodIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("intervention_id",interventionId);
                map.put("food_id",foodId);
                interventionDao.setInterventionAndFood(map);
            }
        }
    }

}
